{include file="../application/admin/view/header.html"}
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-12">
	<div class="alert kesn_prompt"></div>
</div>

	<button class="btn btn-info" id="add_announcement" data-title="新增公告" data-id="add_announcement">新增公告<span class="fui-plus btn-tip"></span></button>

	 <form class="navbar-form navbar-right" action="index" role="search"  method="get">
	  <div class="form-group">
	    <div class="input-group input-group-rounded">
	      <input class="form-control" id="keyword" name="keyword" type="search" placeholder="请输入公告名">
	      <span class="input-group-btn">
	        <button type="submit" class="btn kesn_search"><span class="fui-search"></span></button>
	      </span>
	    </div>
	  </div>
	</form>

</div><br/>
&nbsp;&nbsp;<br/>
<div></div>
<div class="row">
	<table class="table table-hover">
		<thead>
			<th>ID</th>
			<th>公告</th>
			<th>创建时间</th>
			<th>操作</th>
			<th>是否有详情页</th>
		</thead>
		<tbody>
			{volist name="announcement_list" id="announcement"}
			<tr class="_item"> 
				<th scrope="row">{$announcement.id}</th>
				<td class="col-sm-2 col-md-2 announcement_name">{$announcement.announcement}</td>
				<th class="created">{$announcement.created}</th>
				<td class="change_button"><div>
					<a href="#" class="btn btn-info edit_announcement btn-sm" data-status="0">编辑<span class="fui-new btn-tip"></span></a>
					<a href="#" class="btn btn-primary kesn_to btn-sm" data-id="edit_content" data-title="{$announcement.announcement}公告" data-url="/index.php/admin/announcement/save_content_view?announcement_id={$announcement.id}">编辑详情<span class="fui-new btn-tip"></span></a>
					<a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="{$announcement.id}" data-_name="{$announcement.announcement}">删除<span class="fui-cross btn-tip"></span></a>
				</div></td>
				<td class="has_content_td">
					<span class="mrl">
            			<div class="bootstrap-switch bootstrap-switch-wrapper bootstrap-switch-animate bootstrap-switch-id-custom-switch-07 bootstrap-switch-{$announcement.has_content}" data-_module_id="{$announcement.id}"><div class="bootstrap-switch-container"><span class="bootstrap-switch-handle-on bootstrap-switch-success">有</span><label class="bootstrap-switch-label">&nbsp;</label><span class="bootstrap-switch-handle-off bootstrap-switch-default">无</span><input type="checkbox" checked="" data-toggle="switch" name="switch-07" data-on-color="success" id="custom-switch-07"></div>
            			</div>
          			</span>
				</td>
		
		</tr>
		{/volist}
	</tbody>
</div>
{$page}	
</div>

{include file="../application/admin/view/footer.html"}
<script type="text/javascript">
//删除
$('tbody').on('click','.kesn_del',function(){
	var _this = $(this);
	var _id = _this.attr('data-_id');
	var _name = _this.attr('data-_name');
	var _url = '/index.php/admin/announcement/del_announcement';
	kesn_del(_id,_url,'您确实要删除'+_id+' : '+_name+' 吗？',_this);
});
//新增
$('#add_announcement').click(function(){
	var _html = '<tr class="_item"> <th scrope="row"></th><td class="announcement_name"><input type="text" class="form-control" placeholder="" name="announcement_name" value=""/></td><td class="created"></td><td class="change_button"><div><a href="#" class="btn btn-info sure_announcement btn-sm">确认<span class="fui-check-circle btn-tip"></span></a><a href="#" class="btn btn-default cancle_announcement btn-sm">取消<span class="fui-trash btn-tip"></span></a></div></td><td class="has_content_td"></td></tr>';
	$('tbody').append(_html);
});
//确认
$('tbody').on('click','.sure_announcement',function(){
	var _this = $(this);
	var _this_item = _this.parents('._item');
	var announcement_name = _this_item.find('input[name=announcement_name]').val();
	announcement_name = $.trim(announcement_name);
	if (announcement_name == '') {
		alert('公告名不能为空！');
		return;
	}
	  $.ajax({
	       url: "add_announcement",    //请求的url地址
	      dataType: "json",   //返回格式为json
	      async: true, //请求是否异步，默认为异步，这也是ajax重要特性
	      data: {"announcement_name" :announcement_name},    //参数值
	      type: "POST",   //请求方式
	      success: function(data) {
	      	if (data.err == '0') {
	      		// _this_item.find('input[name=warehouse_name]').attr('disabled','disabled');
	      		_this_item.find('.announcement_name').html(announcement_name);
	      		_this_item.find('th[scrope=row]').html(data.announcement_id);
	      		var created = new Date();
	      		var created_year = created.getFullYear();
	      		var created_month = created.getMonth();
	      		var created_day = created.getDate();
	      		created = created_year+"-"+created_month+"-"+created_day;
	      		_this_item.find('.created').html(created);
	      		var change_button_html = '<div><a href="#" class="btn btn-info edit_announcement btn-sm" data-status="0">编辑<span class="fui-new btn-tip"></span></a><a href="#" class="btn btn-primary kesn_to btn-sm" data-id="edit_content" data-title="'+announcement_name+'公告" data-url="/index.php/admin/announcement/save_content_view?announcement_id='+data.announcement_id+'">编辑详情<span class="fui-new btn-tip"></span></a><a href="#" class="btn btn-danger kesn_del btn-sm" data-_id="'+data.announcement_id+'" data-_name="'+announcement_name+'">删除<span class="fui-cross btn-tip"></span></a></div>';
	        	_this_item.find('.change_button').html(change_button_html);
	        	var has_content_html = '<span class="mrl">'
            			+'<div class="bootstrap-switch bootstrap-switch-wrapper bootstrap-switch-animate bootstrap-switch-id-custom-switch-07 bootstrap-switch-off" data-_module_id="'+data.announcement_id+'"><div class="bootstrap-switch-container"><span class="bootstrap-switch-handle-on bootstrap-switch-success">ON</span><label class="bootstrap-switch-label">&nbsp;</label><span class="bootstrap-switch-handle-off bootstrap-switch-default">OFF</span><input type="checkbox" checked="" data-toggle="switch" name="switch-07" data-on-color="success" id="custom-switch-07"></div>'+
            			'</div>'+
          			'</span>';
          		_this_item.find('.has_content_td').html(has_content_html);
	        	kesn_prompt('success',data.content);
	      	}else{
	      		kesn_prompt('fail',data.content);
	      	}
	        

	      },
	  });
	
});

//取消
$('tbody').on('click','.cancle_announcement',function(){
	var _this = $(this);
	_this.parents('._item').remove();
});

//编辑
$('tbody').on('click','.edit_announcement',function(){
	var _this = $(this);
	var _this_item = _this.parents('._item');
	var _status = _this.attr('data-status');
	if (_status == '0') {
		// _this_item.find('input[name=warehouse_name]').removeAttr('disabled');
		var announcement_name = $.trim(_this_item.find('.announcement_name').html());
		_this_item.find('.announcement_name').html('<input type="text" class="form-control input-sm" placeholder="" name="announcement_name" value="'+announcement_name+'"/>');
		_this.html('确认<span class="fui-check btn-tip"></span>');
		_this.attr('data-status','1');
	}else{
		var announcement_id = _this_item.find('th[scrope=row]').html();
		var announcement_name = _this_item.find('input[name=announcement_name]').val();
		if ($.trim(announcement_name) == '') {
			alert('公告名不能为空！');
			return;
		}
		  $.ajax({
		       url: "edit_announcement",    //请求的url地址
		      dataType: "json",   //返回格式为json
		      async: true, //请求是否异步，默认为异步，这也是ajax重要特性
		      data: {"announcement_id":announcement_id,"announcement_name":announcement_name },    //参数值
		      type: "POST",   //请求方式
		      success: function(data) {
		        if (data.err == '0') {
		        	// _this_item.find('input[name=warehouse_name]').attr('disabled','disabled');
		        	_this_item.find('.announcement_name').html(announcement_name);
					_this.html('编辑<span class="fui-new btn-tip"></span>');
					_this.attr('data-status','0');
					kesn_prompt('success',data.content);
		        }else{
		        	kesn_prompt('fail',data.content);
		        }
		      }, 
		});
	}
});

$('tbody').on('click','.bootstrap-switch',function(){
	var _this = $(this);
	var announcement_id = _this.parents('._item').find('th[scrope=row]').html();
	var has_content ;
	if (_this.hasClass('bootstrap-switch-on')){
		has_content = '0';
	}else if (_this.hasClass('bootstrap-switch-off')) {
		has_content= '1';
	}
	$.ajax({
       url: "update_has",    //请求的url地址
	      dataType: "json",   //返回格式为json
	      async: true, //请求是否异步，默认为异步，这也是ajax重要特性
	      data: { "announcement_id": announcement_id,"has" : has_content },    //参数值
	      type: "GET",   //请求方式
	      success: function(data) {
	        if (data.err == '0') {
	        	if (has_content == '0') {
	        		_this.removeClass('bootstrap-switch-on');
					_this.addClass('bootstrap-switch-off');
	        	}else if(has_content == '1'){
	        		_this.removeClass('bootstrap-switch-off');
					_this.addClass('bootstrap-switch-on');
	        	}
	        	kesn_prompt('success',data.content);
	        }else{
	        	kesn_prompt('fail',data.content);
	        }
	      }
	});
});

</script>
</footer>
</body>
</html>